<template>
  <div class="big-box">
    <div class="search-container">
      <el-form
        ref="queryFormRef"
        :inline="true"
        :model="queryParams"
        label-width="100px"
        class="workID"
      >
        <el-form-item label="作文号" prop="workId">
          <el-input
            v-model="queryParams.workId"
            placeholder="作文号"
            clearable
            style="width: 200px; margin-right: 20px"
            @keyup.enter="handleQuery"
        /></el-form-item>
        <el-form-item class="handleQuery">
          <el-button type="primary" @click="handleQuery"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="resetQuery"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-card shadow="never" class="table-container">
      <el-table
        v-loading="loading"
        ref="StudentFormRef"
        :data="StudentList"
        highlight-current-row
        border
      >
        <el-table-column
          label="作文号"
          prop="workId"
          width="120"
          align="center"
        />
        <!-- show-overflow-tooltip -->
        <el-table-column label="作文题目" align="center" prop="tile" />
        <el-table-column fixed="right" label="操作" align="center" width="200">
          <template #default="scope">
            <el-button
              type="primary"
              link
              size="small"
              @click="openDialog(scope.row.username, scope.row.workId)"
              ><i-ep-view />写作</el-button
            >
            <el-button
              type="primary"
              link
              size="small"
              @click="openDialog(scope.row.username, scope.row.workId)"
              ><i-ep-edit />修改</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.page"
        v-model:limit="queryParams.pageSize"
        @pagination="handleQuery"
      />
    </el-card>

    <!-- 写作弹窗 -->
    <el-dialog
      width="700px"
      @close="closeDialogs"
      title="作文内容"
      v-model="dialog.visible"
    >
      <el-form ref="StudentFormRef" :model="formData">
        <el-form-item label="内容" prop="content">
          <el-input
            v-model="formData.content"
            placeholder="请输入写作内容"
            type="textarea"
            style="height: 350px"
            :autosize="{ minRows: 15, maxRows: 15 }"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleSubmit">提 交</el-button>
          <el-button @click="closeDialogs">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { ref } from "vue";
import {
  getStudentComPage,
  getStudentComMenuIds,
  addStudentCom,
  updateStudentCom,
  getUserIds,
} from "@/api/composition/student/index";
import {
  StudentComForms,
  StudentComQuery,
  StudentComPageVO,
  StudentComPageResult,
  UserPageVO,
} from "@/api/composition/student/types";

const value = ref();
const editDialog = ref();
const removeIds = ref([]); // 删除用户ID集合 用于批量删除
const loading = ref(false);
const StudentList = ref<StudentComPageVO[]>();
const userList = ref<UserPageVO[]>();
const total = ref(0);
const queryFormRef = ref(ElForm);
const queryParams = reactive<StudentComQuery>({
  workId: "",
  page: 1,
  pageSize: 10,
});
const UserPageVOs = reactive<UserPageVO>({});

import { useUserStore } from "@/store/modules/user";
const { user } = useUserStore();
console.log(user);

/** 查询 */
function handleQuery() {
  loading.value = true;
  getStudentComPage(queryParams)
    .then(({ data }) => {
      StudentList.value = data.items;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}
/** 重置查询 */
function resetQuery() {
  queryFormRef.value.resetFields();
  queryParams.page = 1;
  handleQuery();
}

onMounted(() => {
  handleQuery();
});

const StudentFormRef = ref(ElForm);

const formData = reactive<StudentComForms>({
  id: "",
  username: "",
  workId: "",
  content: "",
  tile: "",
});
/** 关闭新增修改表单弹窗 */
function closeDialogs() {
  dialog.visible = false;
  resetForm();
}

/** 重置表单 */
function resetForm() {
  StudentFormRef.value.resetFields();
  StudentFormRef.value.clearValidate();

  formData.id = "";
  formData.username = "";
  formData.workId = "";
  formData.content = "";
  formData.tile = "";
}

const dialog = reactive({
  title: "",
  visible: false,
});

/** 打开写作表单弹窗 */
function openDialog(username?: string, workId?: string) {
  dialog.visible = true;
  // console.log(id.id);
  console.log(workId);
  // const ID = id.id;
  const ID = workId;
  const usernames = user.username;
  const jsonData = {
    username: usernames,
    workId: ID,
  };
  if (ID) {
    dialog.title = "作文内容";
    getStudentComMenuIds(jsonData).then(({ data }) => {
      Object.assign(formData, data);
    });
  } else {
    dialog.title = "写作";
  }
}

/** 新增修改保存提交 */
function handleSubmit() {
  // teacherFormRef.value.validate((valid: any) => {
  //   if (valid) {

  //   }
  // });
  ElMessageBox.confirm("确认增加?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    const username = user.username;
    formData.username = username;
    updateStudentCom(formData)
      .then(() => {
        ElMessage.success("新增成功");
        closeDialogs();
        resetQuery();
      })
      .finally(() => (loading.value = false));
  });
}

// /** 写作保存提交 */
// function handleSubmit() {
//   StudentFormRef.value.validate((valid: any) => {
//     if (valid) {
//       loading.value = true;
//       const ID = formData.workId;
//       if (ID) {
//         ElMessageBox.confirm("确认修改?", "警告", {
//           confirmButtonText: "确定",
//           cancelButtonText: "取消",
//           type: "warning",
//         }).then(() => {
//           const username = user.username;
//           formData.username = username;
//           updateStudentCom(username, formData)
//             .then(() => {
//               ElMessage.success("修改成功");
//               closeDialogs();
//               resetQuery();
//             })
//             .finally(() => (loading.value = false));
//         });
//       } else {
//         ElMessageBox.confirm("确认增加?", "警告", {
//           confirmButtonText: "确定",
//           cancelButtonText: "取消",
//           type: "warning",
//         }).then(() => {
//           const username = user.username;
//           formData.username = username;
//           const Id = workId.workId;
//           formData.workId = Id;
//           addStudentCom(formData)
//             .then(() => {
//               ElMessage.success("新增成功");
//               closeDialogs();
//               resetQuery();
//             })
//             .finally(() => (loading.value = false));
//         });
//       }
//     }
//   });
// }
</script>

<style scoped lang="scss">
.form-container {
  display: flex;
  flex-wrap: wrap;
}

.handleQuery {
  margin-left: auto;
}
</style>
